//
//  BBSController.m
//  网易新闻
//
//  Created by dllo on 15/10/9.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "BBSController.h"
#import "MJRefresh.h"
#import "AFTool.h"
#import "BBSCell.h"
#import "BBSModel.h"
#import "Settings.h"
@interface BBSController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *modArr;
@property(nonatomic, retain)NSMutableArray *hotArr;
@property(nonatomic, assign)NSInteger cellheight;
@end
@implementation BBSController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        self.hotArr = [[NSMutableArray alloc]init];
        self.modArr = [[NSMutableArray alloc]init];
        [self createTableView];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewWillLayoutSubviews{
    self.tableView.contentOffset = CGPointMake(0, 0);
    [self.tableView.footer beginRefreshing];
    [self handelHotData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 65, 375, 600) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    [self.tableView registerClass:[BBSCell class] forCellReuseIdentifier:@"BBSCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self handelData];
        [self.tableView.footer endRefreshing];
    }];
}

- (void)handelHotData{
    NSString *urlStr = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/hot/%@/%@/0/10/10/2/2",self.replyBoard,self.docid];
    [AFTool GETWithURL:urlStr body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if ([[tempDic objectForKey:@"hotPosts"] isKindOfClass:[NSArray class]]) {
            [self.hotArr addObjectsFromArray:[tempDic objectForKey:@"hotPosts"]];
        }
        [tempDic release];
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"404(BBSController)");
    }];
}

- (void)handelData{
    NSString *urlStr = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/normal/%@/%@/desc/%lu/20/10/2/2",self.replyBoard,self.docid,self.modArr.count];
    [AFTool GETWithURL:urlStr body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if ([[tempDic objectForKey:@"newPosts"] isKindOfClass:[NSArray class]]) {
            [self.modArr addObjectsFromArray:[tempDic objectForKey:@"newPosts"]];
        }
        [tempDic release];
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"404(BBSController)");
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.hotArr.count;
    }else{
        return self.modArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *headerView = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, 75, 25)];
    headerView.backgroundColor = [UIColor redColor];
    headerView.textColor = [UIColor whiteColor];
    if (section == 0) {
        headerView.text = @"热门跟帖";
    }else{
        headerView.text = @"最新跟帖";
    }
    UIView *headerBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 30)];
    headerBack.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    [headerBack addSubview:headerView];
    [headerView release];
    return [headerBack autorelease];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BBSCell *cell = [[BBSCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BBSCell"];
    if (indexPath.row < self.hotArr.count) {
        BBSModel *mod = [BBSModel modWithDic:[self.hotArr[indexPath.row]objectForKey:@"1"]];
        cell.mod = mod;
    }else{
        BBSModel *mod = [BBSModel modWithDic:[self.modArr[indexPath.row - self.hotArr.count]objectForKey:@"1"]];
        cell.mod = mod;
    }
    self.cellheight = cell.height;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"didSelectRowAtIndexPath%lu", indexPath.row);
}

- (void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_modArr release];
    [_hotArr release];
    [_tableView release];
    [_replyBoard release];
    [_docid release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end